package it.uniroma2.orchestrator;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class GestoreFile {
	private ObjectOutputStream objOutput; 
	private ObjectInputStream objInput;
	private File fileSerial;
	
	GestoreFile(){
		this.fileSerial = new File("regole.dat");
		try {
			this.fileSerial.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void salvaOggetto(Object o){
		try {
			this.objOutput = new ObjectOutputStream(new FileOutputStream(this.fileSerial));
			this.objOutput.writeObject(o);
			this.objOutput.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Object caricaOggetto(){
		Object res = null;
		
		try {
			FileInputStream f = new FileInputStream(this.fileSerial);
			this.objInput = new ObjectInputStream(f);
			res =  this.objInput.readObject();
			this.objInput.close();
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (EOFException e) {
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return res;
	}
}
